﻿using CPPEI.Coolzon.UI.XProperty.Converters;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CPPEI.Coolzon.UI.XProperty
{
    public class PropertyValueConverterFactory
    {
        private static Dictionary<Type, IPropertyValueConverter> s_converterDictionary;
        
        private static UnityContainer _valueConvertorsContainer;
        private static UnityContainer _valueEditorsContainer;
        /// <summary>
        /// 初始化，预加载转换器
        /// </summary>
        public static void Init(string configFilePath)
        {
            if (_valueConvertorsContainer == null)
            {
                _valueConvertorsContainer = new UnityContainer();
                _valueEditorsContainer = new UnityContainer();

                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = configFilePath;

                System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection section = (UnityConfigurationSection)config.GetSection("XProperty");

                section.Configure(_valueConvertorsContainer, "ValueConvertors");
                section.Configure(_valueEditorsContainer, "ValueEditors");
            }
            else
            {
                throw new Exception("has inited");
            }
        }

        /// <summary>
        /// 获取值转换器
        /// </summary>
        /// <param name="valueType"></param>
        /// <returns></returns>
        public static IPropertyValueConverter GetConverter(Type valueType)
        {
            string valueTypeString = valueType.FullName;
            try
            {
                if (_valueConvertorsContainer == null)
                {
                    Init(string.Format("{0}\\Config\\XProperty.xml",
                    AppDomain.CurrentDomain.BaseDirectory));
                }
                if (_valueConvertorsContainer.IsRegistered<IPropertyValueConverter>(valueTypeString))
                {
                    return _valueConvertorsContainer.Resolve<IPropertyValueConverter>(valueTypeString);
                }
                else
                {
                    Common.Logger.ErrorToTag("PropertyValueConverterFactory", "GetConverter转换器解析失败,FullName：{0}", valueTypeString);
                    return null;
                }
            }
            catch(Exception ex)
            {
                Common.Logger.ErrorToTag("PropertyValueConverterFactory", "GetConverter转换器解析失败,FullName：{0},ex:{1}", valueTypeString, ex);
                return null;
            }
        }

        /// <summary>
        /// 获取值的编辑器
        /// </summary>
        /// <param name="valueType"></param>
        /// <returns></returns>
        public static XPropertyValueEditor GetEditor(Type valueType)
        {
            string valueTypeString = valueType.FullName;
            try
            {
                if (_valueEditorsContainer == null)
                {
                    Init(string.Format("{0}\\Config\\XProperty.xml",
                    AppDomain.CurrentDomain.BaseDirectory));
                }
                if (_valueEditorsContainer.IsRegistered<XPropertyValueEditor>(valueTypeString))
                {
                    return _valueEditorsContainer.Resolve<XPropertyValueEditor>(valueTypeString);
                }
                else
                {
                    return new Editors.XDefaultErrorEditor(valueTypeString);
                }
            }
            catch (Exception ex)
            {
                Common.Logger.ErrorToTag("PropertyValueConverterFactory", "GetEditor转换器解析失败,FullName：{0},ex:{1}", valueTypeString, ex);
                return new Editors.XDefaultErrorEditor(valueTypeString);
            }
        }
    }
}
